package com.bdqn.t382.controller;

import com.bdqn.t382.pojo.SysUser;
import com.bdqn.t382.service.SysUserService;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<SysUser> list = sysUserService.findList(new SysUser());
        model.addAttribute("list", list);
        return "list";
    }

    @PostMapping("/add")
    public String add(Model model, SysUser sysUser) {
        int saveBatch = sysUserService.saveBatch(sysUser);
        if (saveBatch > 0) {
            return "redirect:/user/list";
        } else {
            return "redirect:/error.jsp";
        }
    }

    @RequestMapping("/addUser")
    public String addUser() {
        return "addUser";
    }

    @GetMapping("/delete")
    public String delete(Integer id) {
        int count = sysUserService.deleteBatch(id);
        if (count > 0) {
            return "redirect:/user/list";
        } else {
            return "redirect:/error.jsp";
        }
    }

    @GetMapping("/view")
    public String view(Model model, Integer id) {
        SysUser sysUser = sysUserService.getById(id);
        model.addAttribute("sysUser", sysUser);
        return "view";
    }

    @GetMapping("/toUpdate")
    public String toUpdate(Model model, Integer id) {
        SysUser sysUser = sysUserService.getById(id);
        model.addAttribute("sysUser", sysUser);
        return "update";
    }

    @PostMapping("/update")
    public String update(SysUser sysUser) {
        int saveBatch = sysUserService.updateBatch(sysUser);
        if (saveBatch > 0) {
            return "redirect:/user/list";
        } else {
            return "redirect:/error.jsp";
        }
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(SysUser sysUser, HttpSession session) {
        SysUser user;
        if (sysUser.getAccount().length() < 1 || sysUser.getAccount() == null) {
            return "redirect:/error.jsp?msg=null";
        } else {
            user = sysUserService.getSysUserByAccountBiz(sysUser.getAccount());
            if (user == null) {
                logger.info(sysUser.getAccount() + "用户不存在");
                //用户不存在
                return "redirect:/error.jsp?msg=userNoExists";
            } else if (!user.getPassword().equals(sysUser.getPassword())) {
                //密码不正确
                logger.info(sysUser.getAccount() + "密码错误");
                return "redirect:/error.jsp?msg=passwordError";
            } else {
                logger.info(sysUser.getAccount() + "登录成功");
                //验证成功设置会话
                session.setAttribute("user", user);
                return "redirect:/user/list";
            }
        }
    }
}
